<?php
/**
 * Widget Name: Popular Posts with a Thumbnail
 * Description: A Popular Posts widget that displays a thumbnail from your blog.
 * Version: 1.0
 */
 
class PopularWidget extends WP_Widget {
   
    function PopularWidget() {
        parent::WP_Widget(false, $name = 'GP Popular Posts');	
    }

    function widget($args, $instance) {		
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        ?>
		
	<?php echo $before_widget; ?>
		<?php if ( $title )
			 echo $before_title . $title . $after_title; ?>
				<ul>                  
				<div class="widget-popular-posts">
					<?php $pc = new WP_Query('orderby=comment_count&posts_per_page=3'); ?>
						<?php while ($pc->have_posts()) : $pc->the_post(); ?>
							<?php
							$thumb = get_post_thumbnail_id(); 
							$postimage = vt_resize($thumb, '', 60, 60, true ); // resize thumbnail using method from Victor Teixeira
							?> 
					<li>
						<div class="pp-wrap clearfix">	
							<?php if (has_post_thumbnail()) : ?>
							
								<a href="<?php the_permalink(); ?>"><img class="imageborder" src="<?php echo $postimage[url]; ?>" width="<?php echo $postimage[width]; ?>" height="<?php echo $postimage[height]; ?>" alt="<?php the_title(); ?>" /></a>
							
							<?php else : ?>
							
							
								<a href="<?php the_permalink(); ?>"><img class="imageborder" src="<?php get_template_directory_uri(); ?>/images/noimage.png" width="<?php echo $postimage[width]; ?>" height="<?php echo $postimage[height]; ?>" alt="<?php the_title(); ?>"/></a>
							
							<?php endif; ?>
				
							<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
							<p class="pp-meta">Posted: <?php echo get_the_date(); ?><br />
							<?php comments_popup_link( __('No Comments', '1 Comment', '% Comments') ); ?></p>		
						</div>
					</li>
					<?php endwhile; ?>
				</div></ul>                  
              <?php echo $after_widget; ?>
        <?php
    }

/** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
	$instance = $old_instance;
	$instance['title'] = strip_tags($new_instance['title']);
        return $instance;
    }

/** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
         <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'gp'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <?php 
    }

}

// register widget
add_action('widgets_init', create_function('', 'return register_widget("PopularWidget");'));
